<template>
  <div class="main_spac1">
    <div class="main_white padding10">
      <div class="mainor_form small-top">
        <el-form
          :inline="true"
          :model="form"
          class="demo-form-inline"
          ref="userListFrom"
          label-width="90px"
          id="form"
        >
          <el-form-item label="商品名称：" prop="name">
            <el-input
              v-model.trim="form.name"
              placeholder="商品名称"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="分类：" prop="category_id" class="wid220">
            <el-cascader
              v-model="form.category_id"
              :options="options"
              :props="defaultProps"
              @change="getArticleList"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="" prop="bind_type">
            <el-select
              v-model.trim="form.bind_type"
              placeholder="采购方式"
              class="wid100"
              clearable
            >
              <el-option
                v-for="(modeList, index) in info.modeList"
                :key="index"
                :label="modeList"
                :value="parseInt(index)"
              ></el-option>
            </el-select>
            <el-select
              v-if="form.bind_type == 1"
              v-model.trim="form.buyer_id"
              placeholder="采购员"
              class="wid220"
              filterable
              clearable
              @change="getArticleList"
            >
              <el-option
                v-for="(item, key) in info.buyer"
                :key="key"
                :label="item"
                :value="key"
              ></el-option>
            </el-select>
            <el-select
              v-else-if="form.bind_type == 2"
              v-model.trim="form.supplier_id"
              placeholder="供应商"
              class="wid220"
              filterable
              clearable
            >
              <el-option
                v-for="(item, key) in info.supplier"
                :key="key"
                :label="item"
                :value="key"
              ></el-option>
            </el-select>
            <el-select
              v-else-if="form.bind_type == 3"
              v-model.trim="form.direct_supply"
              placeholder="直供用户"
              class="wid220"
              filterable
              @change="getArticleList"
              clearable
            >
              <el-option label="所有" value=""></el-option>
              <el-option
                v-for="(direct_supply, index) in info.direct_supply"
                :key="index"
                :label="direct_supply"
                :value="parseInt(index)"
              ></el-option>
            </el-select>
          </el-form-item>
          <MainFormBtn @reset="reset" @getSearchFor="getArticleList" :loading="listLoading" :showBtn="false">
          </MainFormBtn>
          <!-- <el-select
						v-show="!form.bind_type"
						v-model.trim="form_mode_all"
						placeholder="所有"
						class="wid220"
					>
						<el-option label="所有" value=""></el-option>
					</el-select> -->
          <!-- <div class="clearfix"></div> -->
        </el-form>
        <div class="main-controls-wrap">
          <div>
            <el-button @click="binding(1)">批量绑定采购员</el-button>
            <el-button @click="binding(2)">批量绑定供应商</el-button>
          </div>
          <div class="flex-center">
            <el-checkbox v-model="type" @change="checkbox">启用用户关系</el-checkbox>
            <el-tooltip class="item" effect="dark" placement="bottom">
              <div slot="content">
                启用后采购员，供应商可绑定采购
                <br />分组或用户
              </div>
              <svg aria-hidden="true" class="navbar_svg" width="16" height="16">
                <use xlink:href="#icon-summary_question" />
              </svg>
            </el-tooltip>
          </div>
        </div>
      </div>
      <div class="main_table row-expand-down">
        <el-table
          ref="multipleTable"
          v-loading="listLoading"
          :data="list.list.data"
          element-loading-text="加载中"
          fit
          border
          highlight-current-row
          @selection-change="handleSelectionChange"
          :height="borderHeight"
          :header-cell-class-name="this.$verticaline"
          :header-cell-style="tableHeightStyle"
          @header-dragend="changeTableWidth"
        >
          <!-- :row-class-name="getRowClass" -->
          <el-table-column type="expand" width="1">
            <template slot-scope="props">
              <div class="maincg_ul">
                <ul class="boxha">
                  <li
                    class="box"
                    v-for="(item, index) in props.row.purchase_list"
                    :key="index"
                  >
                    <div class="maincg_uli box">
                      <template v-if="item.id">
                        <template v-if="item.enable_user_relationship != 0">
                          <template v-if="item.bind_type == 2">
                            <el-select
                              v-model.trim="item.supply_type"
                              placeholder="供应方式"
                              class="wid110"
                              @change="
                                purchaseRelation_editBind(
                                  props.$index,
                                  index,
                                  item.supply_type,
                                  1
                                )
                              "
                            >
                              <el-option
                                v-for="(
                                  supply_types, index
                                ) in info.supply_types"
                                :key="index"
                                :label="supply_types"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select>
                            <el-select
                              v-model.trim="item.supplier_id"
                              placeholder="供应商"
                              class="wid110 marginLeft5"
                              @change="
                                purchaseRelation_editBind(props.$index, index)
                              "
                            >
                              <template v-if="item.supply_type == 1">
                                <el-option
                                  v-for="(supplier, index) in info.supplier"
                                  :key="index"
                                  :label="supplier"
                                  :value="parseInt(index)"
                                ></el-option>
                              </template>
                              <template v-if="item.supply_type == 2">
                                <el-option
                                  v-for="(
                                    supplier, index
                                  ) in info.direct_supply"
                                  :key="index"
                                  :label="supplier"
                                  :value="parseInt(index)"
                                ></el-option>
                              </template>
                            </el-select>
                          </template>
                          <template v-else>
                            <el-select
                              v-model.trim="item.buyer_id"
                              placeholder="采购员"
                              class="wid110"
                              @change="
                                purchaseRelation_editBind(props.$index, index)
                              "
                            >
                              <el-option
                                v-for="(buyer, index) in info.buyer"
                                :key="index"
                                :label="buyer"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select>
                          </template>
                          <el-select
                            v-model.trim="item.user_type"
                            placeholder="采购分组"
                            class="wid180 marginLeft5"
                            @change="
                              (item.user_id = null),
                                (item.purchase_group_id = null)
                            "
                          >
                            <el-option
                              v-for="(user_types, index) in info.user_types"
                              :key="index"
                              :label="user_types"
                              :value="parseInt(index)"
                            ></el-option>
                          </el-select>

                          <el-select
                            v-model.trim="item.purchase_group_id"
                            placeholder="采购分组"
                            class="wid180 marginLeft5"
                            v-if="item.user_type == 1"
                            @change="
                              purchaseRelation_editBind(props.$index, index)
                            "
                          >
                            <el-option
                              v-for="(
                                purchase_group, index
                              ) in info.purchase_group"
                              :key="index"
                              :label="purchase_group"
                              :value="parseInt(index)"
                            ></el-option>
                          </el-select>
                          <el-select
                            v-model.trim="item.user_id"
                            placeholder="用户"
                            class="wid180 marginLeft5"
                            filterable
                            v-if="item.user_type == 2"
                            @change="
                              purchaseRelation_editBind(props.$index, index)
                            "
                          >
                            <el-option
                              v-for="(members, index) in info.members"
                              :key="index"
                              :label="members"
                              :value="parseInt(index)"
                            ></el-option>
                          </el-select>
                        </template>
                        <template v-else>
                          <template v-if="item.bind_type == 2">
                            <!-- <el-select
                              v-model.trim="item.supply_type"
                              placeholder="供应方式"
                              class="wid110"
                              @change="
                                purchaseRelation_editBind(props.$index, index)
                              "
                            >
                              <el-option
                                v-for="(
                                  supply_types, index
                                ) in info.supply_types"
                                :key="index"
                                :label="supply_types"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select> -->
                            <el-select
                              v-if="item.supply_type == 1"
                              v-model.trim="item.supplier_id"
                              placeholder="普通供应商"
                              class="wid160 marginLeft5"
                              @change="
                                purchaseRelation_editBind(props.$index, index)
                              "
                            >
                              <el-option
                                v-for="(supplier, index) in info.supplier"
                                :key="index"
                                :label="supplier"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select>
                            <el-select
                              v-if="item.supply_type == 2"
                              v-model.trim="item.supplier_id"
                              placeholder="直供供应商"
                              class="wid160 marginLeft5"
                              @change="
                                purchaseRelation_editBind(props.$index, index)
                              "
                            >
                              <el-option
                                v-for="(supplier, index) in info.direct_supply"
                                :key="index"
                                :label="supplier"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select>
                          </template>
                          <template v-else>
                            <el-select
                              v-model.trim="item.buyer_id"
                              placeholder="采购员"
                              class="wid110"
                              @change="
                                purchaseRelation_editBind(props.$index, index)
                              "
                            >
                              <el-option
                                v-for="(buyer, index) in info.buyer"
                                :key="index"
                                :label="buyer"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select>
                          </template>
                        </template>
                      </template>
                      <template v-else>
                        <el-select
                          v-model.trim="item.select"
                          placeholder="请选择"
                          class="wid110"
                        >
                          <el-option label="自采" :value="1"></el-option>
                          <el-option label="供应商" :value="2"></el-option>
                        </el-select>
                        <template v-if="item.enable_user_relationship != 0">
                          <template v-if="item.select == 2">
                            <el-select
                              v-model.trim="item.supply_type"
                              placeholder="供应方式"
                              class="wid180"
                              @change="
                                () => {
                                  item.supplier_id = '';
                                }
                              "
                            >
                              <el-option
                                v-for="(
                                  supply_types, index
                                ) in info.supply_types"
                                :key="index"
                                :label="supply_types"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select>
                            <el-select
                              v-model.trim="item.supplier_id"
                              placeholder="供应商"
                              class="wid110 marginLeft5"
                              @change="
                                () => {
                                  item.buyer_id = '';
                                }
                              "
                            >
                              <template v-if="item.supply_type == 1">
                                <el-option
                                  v-for="(supplier, index) in info.supplier"
                                  :key="index"
                                  :label="supplier"
                                  :value="parseInt(index)"
                                ></el-option>
                              </template>
                              <template v-if="item.supply_type == 2">
                                <el-option
                                  v-for="(
                                    supplier, index
                                  ) in info.direct_supply"
                                  :key="index"
                                  :label="supplier"
                                  :value="parseInt(index)"
                                ></el-option>
                              </template>
                            </el-select>
                          </template>
                          <template v-else>
                            <el-select
                              v-model.trim="item.buyer_id"
                              placeholder="采购员"
                              class="wid110"
                              @change="
                                () => {
                                  item.supplier_id = '';
                                }
                              "
                            >
                              <el-option
                                v-for="(buyer, index) in info.buyer"
                                :key="index"
                                :label="buyer"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select>
                          </template>
                          <el-select
                            v-model.trim="item.user_type"
                            placeholder="采购分组"
                            class="wid160 marginLeft5"
                          >
                            <el-option
                              v-for="(user_types, index) in info.user_types"
                              :key="index"
                              :label="user_types"
                              :value="parseInt(index)"
                              @change="
                                () => {
                                  item.purchase_group_id = '';
                                }
                              "
                            ></el-option>
                          </el-select>
                          <el-select
                            v-model.trim="item.purchase_group_id"
                            placeholder="采购分组"
                            class="wid180 marginLeft5"
                            v-if="item.user_type == 1"
                            @change="
                              () => {
                                item.user_id = '';
                              }
                            "
                          >
                            <el-option
                              v-for="(
                                purchase_group, index
                              ) in info.purchase_group"
                              :key="index"
                              :label="purchase_group"
                              :value="parseInt(index)"
                            ></el-option>
                          </el-select>
                          <el-select
                            v-model.trim="item.user_id"
                            placeholder="用户"
                            class="wid180 marginLeft5"
                            filterable
                            v-if="item.user_type == 2"
                            @change="
                              () => {
                                item.purchase_group_id = '';
                              }
                            "
                          >
                            <el-option
                              v-for="(members, index) in info.members"
                              :key="index"
                              :label="members"
                              :value="parseInt(index)"
                            ></el-option>
                          </el-select>
                        </template>
                        <template v-else>
                          <template v-if="item.select == 2">
                            <!-- <el-select
                              v-model.trim="item.supply_type"
                              placeholder="供应方式"
                              class="wid110"
                            >
                              <el-option
                                v-for="(
                                  supply_types, index
                                ) in info.supply_types"
                                :key="index"
                                :label="supply_types"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select> -->
                            <el-select
                              v-model.trim="item.supplier_id"
                              placeholder="供应商"
                              class="wid180 marginLeft5"
                            >
                              <el-option
                                v-for="(supplier, index) in info.supplier"
                                :key="index"
                                :label="supplier"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select>
                          </template>
                          <template v-else>
                            <el-select
                              v-model.trim="item.buyer_id"
                              placeholder="采购员"
                              class="wid110"
                            >
                              <el-option
                                v-for="(buyer, index) in info.buyer"
                                :key="index"
                                :label="buyer"
                                :value="parseInt(index)"
                              ></el-option>
                            </el-select>
                          </template>
                        </template>
                      </template>

                      <div class="maincla_sxf" v-if="type">
                        <svg
                          aria-hidden="true"
                          :class="
                            item.enable_user_relationship == 0
                              ? 'main_page_top main_page_svg'
                              : 'main_page_svg'
                          "
                          width="15"
                          height="15"
                          @click="plus(props.$index)"
                        >
                          <use xlink:href="#icon-commodity_plus" />
                        </svg>
                        <svg
                          aria-hidden="true"
                          class="main_page_svg"
                          width="22"
                          height="22"
                          v-if="item.enable_user_relationship != 0"
                          @click="
                            item.id
                              ? reduce(props.$index, index)
                              : reduce(props.$index, index, true)
                          "
                        >
                          <use xlink:href="#icon-commodity_reduce" />
                        </svg>
                      </div>
                      <div v-if="!item.id">
                        <el-button
                          type="primary"
                          @click="determ(props.$index, index)"
                          >确定</el-button
                        >
                      </div>
                    </div>
                  </li>
                </ul>
              </div>
            </template>
          </el-table-column>

          <el-table-column type="selection" />
          <el-table-column label="商品名称"
          prop="name" :width="tableWidth[hash+'name'] || 340">
            <template slot-scope="scope">{{ scope.row.name }}</template>
          </el-table-column>

          <el-table-column
            label="单位"
            prop="spu[0].spu_unit.title"
            :width="tableWidth[hash+'spu[0].spu_unit.title'] || 240"
            class-name="main_hei"
          />

          <el-table-column label="关系网" prop="purchase_relation" 
          class-name="main_hei"
          :width="tableWidth[hash+'purchase_relation']">
            <template slot-scope="scope">
              <div class="boxha maincg_bj">
                <p
                  v-for="(purchase_relation, index) in scope.row
                    .purchase_relation"
                  :key="index"
                >
                  {{
                    purchase_relation.bind_type == 1
                      ? "市场自采"
                      : purchase_relation.supply_type == 1
                      ? "普通供应"
                      : "直供用户"
                  }}
                  -
                  {{
                    purchase_relation.bind_type == 1
                      ? purchase_relation.buyer
                        ? purchase_relation.buyer.name
                        : ""
                      : purchase_relation.supplier
                      ? purchase_relation.supplier.name
                      : ""
                  }}
                  {{
                    purchase_relation.user_type > 0
                      ? purchase_relation.user_type == 1
                        ? "- 采购分组：" +
                          purchase_relation.purchase_group.title
                        : "- 用户：" + purchase_relation.member.nickname
                      : ""
                  }}
                </p>
              </div>
            </template>
          </el-table-column>

          <el-table-column
            label="操作 "
            class-name="main_hei"
            prop="operate"
            :width="tableWidth[hash+'operate'] || 200"
            align="center"
          >
            <template slot-scope="scope">
              <el-link
                type="success"
                class="maincg_xa"
                @click="formula_meth(scope.row, scope.$index)"
                v-if="!scope.row.down"
                >维护</el-link
              >
              <el-link
                type="success"
                class="maincg_xa"
                @click="formula_meth(scope.row, scope.$index)"
                v-if="scope.row.down"
                >收起</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="main_page boxdq">
        <div></div>
        <!-- 分页 -->
        <div class="main_page_r">
          <MainPagination
            :current-page="current.page"
            :page-sizes="[20, 30, 50, 100]"
            :page-size="limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount"
            background
            @size-change="changeLimit"
            @current-change="changePage"
          />
        </div>
      </div>
    </div>

    <el-dialog
      :title="bindingdata == 1 ? '批量绑定采购员' : '批量绑定供应商'"
      :visible.sync="bindingVisible"
      class="main_dialog dialog840"
      @close="close"
    >
      <el-form
        :inline="true"
        :rules="rules"
        :model="bindform"
        class="demo-form-inline"
        ref="binduserListFrom"
        label-width="5px"
      >
        <template v-if="type">
          <template v-if="bindingdata == 2">
            <el-form-item label=" ">
              <el-select
                v-model.trim="bindform.supply_type"
                placeholder="供应方式"
                class="wid160"
                @change="changeSupplyType"
              >
                <el-option
                  v-for="(supply_types, index) in info.supply_types"
                  :key="index"
                  :label="supply_types"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label=" " prop="supplier_id">
              <el-select
                v-model.trim="bindform.supplier_id"
                placeholder="供应商"
                class="wid160 marginLeft5"
                @change="purchaseRelation_getGoods"
                filterable
              >
                <el-option
                  v-for="(supplier, index) in info.supplier_list"
                  :key="index"
                  :label="supplier"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item label=" " prop="buyer_id">
              <el-select
                v-model.trim="bindform.buyer_id"
                placeholder="采购员"
                class="wid110"
                @change="purchaseRelation_getGoods"
                filterable
              >
                <el-option
                  v-for="(buyer, index) in info.buyer"
                  :key="index"
                  :label="buyer"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
          </template>
          <el-form-item label=" " prop="user_type">
            <el-select
              v-model.trim="bindform.user_type"
              placeholder="采购分组"
              class="wid160 marginLeft5"
              @change="purchaseRelation_getGoods"
            >
              <el-option
                v-for="(user_types, index) in info.user_types"
                :key="index"
                :label="user_types"
                :value="parseInt(index)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label=" "
            prop="purchase_group_id"
            v-if="bindform.user_type == 1"
          >
            <el-select
              v-model.trim="bindform.purchase_group_id"
              placeholder="采购分组"
              class="wid180 marginLeft5"
              @change="purchaseRelation_getGoods"
              filterable
            >
              <el-option
                v-for="(purchase_group, index) in info.purchase_group"
                :key="index"
                :label="purchase_group"
                :value="parseInt(index)"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label=" " prop="user_id" v-if="bindform.user_type == 2">
            <el-select
              v-model.trim="bindform.user_id"
              placeholder="用户"
              class="wid180 marginLeft5"
              filterable
              @change="purchaseRelation_getGoods"
            >
              <el-option
                v-for="(members, index) in info.members"
                :key="index"
                :label="members"
                :value="parseInt(index)"
              ></el-option>
            </el-select>
          </el-form-item>
        </template>
        <template v-else>
          <template v-if="bindingdata == 2">
            <!-- <el-form-item label=" ">
              <el-select
                v-model.trim="bindform.supply_type"
                placeholder="供应方式"
                class="wid160"
              >
                <el-option
                  v-for="(supply_types, index) in info.supply_types"
                  :key="index"
                  :label="supply_types"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item label="" prop="supplier_id">
              <el-select
                v-model.trim="bindform.supplier_id"
                placeholder="供应商"
                class="wid120"
                @change="purchaseRelation_getGoods"
                filterable
              >
                <el-option
                  v-for="(supplier, index) in info.supplier"
                  :key="index"
                  :label="supplier"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
          </template>
          <template v-else>
            <el-form-item label=" " prop="buyer_id">
              <el-select
                v-model.trim="bindform.buyer_id"
                placeholder="采购员"
                class="wid110"
                @change="purchaseRelation_getGoods"
                filterable
              >
                <el-option
                  v-for="(buyer, index) in info.buyer"
                  :key="index"
                  :label="buyer"
                  :value="parseInt(index)"
                ></el-option>
              </el-select>
            </el-form-item>
          </template>
        </template>
      </el-form>
      <div class="main_table row-expand-down mg-t-6">
        <el-table
          ref="multipleTable1"
          v-loading="listLoading"
          :data="list_sh.list.data"
          element-loading-text="加载中"
          fit
          highlight-current-row
          @selection-change="bindingSelectionChange"
        >
          <el-table-column
            type="selection"
            width="60"
            :selectable="selectInit"
          />
          <el-table-column label="商品名称" prop="name" class-name="main_hei" />
          <el-table-column label="操作 " class-name="main_hei" align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.purchase_relation.length == 0">/</div>
              <div v-else>
                <el-link
                  type="success"
                  @click="
                    (e) => {
                      force(e, scope.row.id, scope.row);
                    }
                  "
                  v-repeatClick
                  >强制绑定</el-link
                >
                <!-- <el-link type="green" @click="cancel(scope.row.id)"
                  >取消</el-link
                > -->
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="(bindingVisible = false), close()">取 消</el-button>
        <el-button type="primary" @click="setupmine" v-repeatClick
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import {
  purchaseRelation_index,
  purchaseRelation_base,
  purchaseRelation_getGoods,
  purchaseRelation_bindBuyer,
  purchaseRelation_forceBind,
  purchaseRelation_bindSupplier,
  purchaseRelation_delete,
  purchaseRelation_editBind,
  purchaseRelation_addBind,
} from "@/api/purchasing/purchasingRelation";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { recursion, category } from "@/utils/index";
import { setting_company } from "@/api/currency";
import rules from "@/rules/purchasing/purchasingRelation";
import MainPagination from '@/components/MainPagination'

const shuju = {
  buyer_id: null,
  supplier_id: null,
  user_type: 1,
  purchase_group_id: null,
  user_id: null,
  select: 1,
};
export default {
  mixins: [mixins],
  components: { MainPagination },
  data() {
    return {
      // 验证
      rules,
      // 批量绑定、解绑
      bindform: {
        buyer_id: null,
        supplier_id: null,
        user_type: 1,
        purchase_group_id: null,
        supply_type: 1,
        user_id: null,
      },
      bindingVisible: false,
      bindingdata: "",
      bindingselectArr: [],
      list_sh: {
        list: {
          data: [],
        },
      },
      list_ids: {
        list: {
          data: [],
        },
      },
      // 全选
      selectArr: [],
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 搜索
      form: {
        bind_type: null,
        name: "",
        pid: "",
        category_id: [],
        buyer_id: "",
        supplier_id: "",
        direct_supply: "",
      },

      type: false,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      info: {
        modeList: {},
        buyer: {},
        supplier: {},
        direct_supply: {},
        supplier_list: {}, //两种供应方式切换
      },
      listLoading: false,
      // 分类
      options: [],
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
    };
  },
  created() {
    // 列表
    this.getNumerical(190);
    this.purchaseRelation_index();
    this.purchaseRelation_base();
  },
  methods: {
    startGetting() {
      this.getArticleList();
    },
    // 关闭清空表单
    close() {
      this.bindform = {
        buyer_id: null,
        supplier_id: null,
        user_type: 1,
        purchase_group_id: null,
        user_id: null,
        supply_type: 1,
      };
      this.list_sh = {
        list: {
          data: [],
        },
      };
      this.list_ids = {
        list: {
          data: [],
        },
      };
      this.$refs["binduserListFrom"].clearValidate();
    },
    // 判断表格是否有子项，无子项不显示展开按钮
    getRowClass(row) {
      if (row.row.purchase_relation) {
        if (row.row.purchase_relation.length === 0) {
          return "maincg_cover";
        }
      }
    },
    // 维护-展开行
    formula_meth(row, index) {
      let $table = this.$refs.multipleTable;
      this.list.list.data[index].down = !this.list.list.data[index].down;
      this.list.list.data.map((item) => {
        if (item.down) {
          $table.toggleRowExpansion(item, true);
        } else {
          $table.toggleRowExpansion(item, false);
        }
      });
    },
    // 监听全选
    handleSelectionChange(val) {
      this.selectArr = [];
      val.map((item, ind) => {
        this.selectArr.push(item.id);
      });
    },
    // 维护-加
    plus(index) {
      this.list.list.data[index].purchase_list.push(
        JSON.parse(JSON.stringify(shuju))
      );
    },
    determ(index, ind) {
      let bind_type = this.list.list.data[index].purchase_list[ind].select;
      let id = this.list.list.data[index].id;
      let buyer_id = this.list.list.data[index].purchase_list[ind].buyer_id;
      let supplier_id =
        this.list.list.data[index].purchase_list[ind].supplier_id;
      let user_type = this.list.list.data[index].purchase_list[ind].user_type;
      let purchase_group_id =
        this.list.list.data[index].purchase_list[ind].purchase_group_id;
      let user_id = this.list.list.data[index].purchase_list[ind].user_id;
      let supply_type =
        this.list.list.data[index].purchase_list[ind].supply_type;
      let obj = {
        bind_type: bind_type,
        goods_id: id,
        buyer_id: buyer_id,
        supplier_id: supplier_id,
        user_type: user_type,
        purchase_group_id: purchase_group_id,
        user_id: user_id,
        supply_type: supply_type,
      };
      if (this.list.list.data[index].purchase_list[ind].select == 1) {
        if (buyer_id == "" || buyer_id == null) {
          this.$message.error("采购员不能为空");
          return;
        }
      } else {
        // if (supply_type == "" || supply_type == null) {
        //   this.$message.error("供应方式不能为空");
        //   return;
        // }
        if (supplier_id == "" || supplier_id == null) {
          this.$message.error("供应商不能为空");
          return;
        }
      }
      if (user_type == 1) {
        if (purchase_group_id == "" || purchase_group_id == null) {
          this.$message.error("采购分组不能为空");
          return;
        }
      } else {
        if (user_id == "" || user_id == null) {
          this.$message.error("用户不能为空");
          return;
        }
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, obj)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        obj
      );
      purchaseRelation_addBind(data).then((res) => {
        if (res.errcode == 0) {
          this.$message.success("添加成功");
          this.purchaseRelation_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 维护-减
    reduce(index, ind, type) {
      if (type) {
        this.list.list.data[index].purchase_list.splice(ind, 1);
        return;
      }
      let obj = {
        id: this.list.list.data[index].purchase_relation[ind].id,
      };
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, obj)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        obj
      );
      purchaseRelation_delete(data).then((res) => {
        if (res.errcode == 0) {
          this.list.list.data[index].purchase_relation.splice(ind, 1);
          this.list.list.data[index].purchase_list.splice(ind, 1);
          this.$message.success("删除成功");
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 维护-选择
    purchaseRelation_editBind(index, ind, supply_type, type) {
      if (type == 1) {
        // 选择供应商切换直供，供应商列表
        this.list.list.data[index].purchase_list[ind].supplier_id = "";
      }

      let obj = {
        id: this.list.list.data[index].purchase_list[ind].id,
        buyer_id: this.list.list.data[index].purchase_list[ind].buyer_id,
        supplier_id: this.list.list.data[index].purchase_list[ind].supplier_id,
        user_type: this.list.list.data[index].purchase_list[ind].user_type,
        purchase_group_id:
          this.list.list.data[index].purchase_list[ind].purchase_group_id,
        user_id: this.list.list.data[index].purchase_list[ind].user_id,
        supply_type: this.list.list.data[index].purchase_list[ind].supply_type,
      };

      if (!obj.supplier_id && type == 1) {
        this.$message.error("请选择供应商");
        return;
      }
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, obj)) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        obj
      );
      purchaseRelation_editBind(data).then((res) => {
        if (res.errcode == 0) {
          this.purchaseRelation_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 监听绑定全选不可勾选
    selectInit(row, index) {
      if (row.purchase_relation.length != 0) {
        return false;
      } else {
        return true;
      }
    },
    // 监听绑定全选
    bindingSelectionChange(val) {
      this.bindingselectArr = [];
      val.map((item) => {
        this.bindingselectArr.push({ id: item.id });
      });
    },
    // 批量绑定确认
    setupmine() {
      this.$refs["binduserListFrom"].validate((valid) => {
        if (valid) {
          if (this.bindingselectArr.length != 0) {
            let data = Object.assign(
              {
                sign: Md5(
                  objKeySort(Object.assign(apiUrl().name, this.form_id())) +
                    apiUrl().appsecret
                ),
              },
              apiUrl().name,
              this.form_id(),
              { goods_ids: JSON.stringify(this.bindingselectArr) }
            );
            if (this.bindingdata == 1) {
              purchaseRelation_bindBuyer(data).then((res) => {
                if (res.errcode == 0) {
                  this.$message({
                    message: "绑定成功",
                    type: "success",
                  });
                  this.purchaseRelation_index();
                  this.bindingVisible = false;
                } else {
                  this.$message.error(res.errmsg);
                }
              });
            } else {
              purchaseRelation_bindSupplier(data).then((res) => {
                if (res.errcode == 0) {
                  this.$message({
                    message: "绑定成功",
                    type: "success",
                  });
                  this.purchaseRelation_index();
                  this.bindingVisible = false;
                } else {
                  this.$message.error(res.errmsg);
                }
              });
            }
          } else {
            this.$message.error("请勾选商品");
          }
        }
      });
    },
    // 强制绑定
    force(e, id, row) {
      // console.log(e.path, row)
      this.$refs["binduserListFrom"].validate((valid) => {
        if (valid) {
          // if (e.path[1].disabled) {
          //   return;
          // }
          let data = Object.assign(
            {
              sign: Md5(
                objKeySort(
                  Object.assign(apiUrl().name, { goods_id: id }, this.form_id())
                ) + apiUrl().appsecret
              ),
            },
            apiUrl().name,
            this.form_id(),
            { goods_id: id }
          );
          purchaseRelation_forceBind(data).then((res) => {
            if (res.errcode == 0) {
              this.purchaseRelation_getGoods();
            } else {
              this.$message.error(res.errmsg);
            }
          });
        }
      });
    },
    form_id() {
      let form = JSON.parse(JSON.stringify(this.bindform));
      let id = "";
      if (this.type) {
        if (this.bindingdata == 1) {
          if (form.user_type == 1) {
            let { supply_type, supplier_id, user_id, ...buyer_form } = form;
            id = buyer_form;
          } else {
            let { supply_type, supplier_id, purchase_group_id, ...buyer_form } =
              form;
            id = buyer_form;
          }
        } else {
          if (form.user_type == 1) {
            let { buyer_id, user_id, ...supplier_form } = form;
            id = supplier_form;
          } else {
            let { buyer_id, purchase_group_id, ...supplier_form } = form;
            id = supplier_form;
          }
        }
      } else {
        if (this.bindingdata == 1) {
          let {
            supply_type,
            supplier_id,
            user_type,
            purchase_group_id,
            user_id,
            ...buyer_form
          } = form;
          id = buyer_form;
        } else {
          let {
            buyer_id,
            user_type,
            purchase_group_id,
            user_id,
            ...supplier_form
          } = form;
          id = supplier_form;
        }
      }
      return id;
    },
    // 强制取消
    cancel(id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { goods_id: id }, this.form_id())
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { goods_id: id },
        this.form_id()
      );
      goodsoffer_offer(data).then((res) => {
        if (res.errcode == 0) {
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 批量绑定选择
    binding(type) {
      let list = JSON.parse(JSON.stringify(this.list.list.data));

      let arry = [];
      this.selectArr.map((e) => {
        arry.push(list.find((c) => c.id == e));
      });
      this.list_ids.list.data = arry;
      this.list_sh.list.data = arry;
      this.bindingdata = type;
      this.bindingVisible = true;
    },
    // 绑定采购员、供应商列表
    purchaseRelation_getGoods() {
      let list = JSON.parse(JSON.stringify(this.list_ids.list.data)).map(
        (e) => e.id
      );
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.form_id(), this.current_sh)
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.form_id(),
        { ids: JSON.stringify(list) },
        this.current_sh
      );
      purchaseRelation_getGoods(data).then((res) => {
        if (res.errcode == 0) {
          this.list_sh = res.data;
          res.data.list.data.map((e) => {
            if (e.purchase_relation.length == 0) {
              this.$nextTick(() => {
                this.$refs.multipleTable1.toggleRowSelection(e, true);
              });
            }
          });
          this.totalCount_sh = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 绑定采购员供应商选择供应方式
    changeSupplyType(val) {
      this.bindform.supplier_id = "";
      switch (val) {
        case 1:
          this.info.supplier_list = this.info.supplier;
          break;
        case 2:
          this.info.supplier_list = this.info.direct_supply;
          break;
      }
    },
    // 新增供应方式的时候切换供应商

    // 搜索勾选
    checkbox() {
      let type = this.type ? 1 : 0;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { enable_user_relationship: type })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { enable_user_relationship: type }
      );
      setting_company(data).then((res) => {
        if (res.errcode == 0) {
          this.current.page = 1;
          this.purchaseRelation_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.purchaseRelation_index();
    },
    changePage(page) {
      this.current.page = page;
      this.purchaseRelation_index();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.purchaseRelation_index();
    },
    reset() {
      this.$refs["userListFrom"].resetFields();
      this.form.supplier_id = null;
      this.form.buyer_id = null;
      this.form.direct_supply = null;
      this.getArticleList();
    },
    // 列表
    purchaseRelation_index() {
      this.listLoading = true;
      let form = JSON.parse(JSON.stringify(this.form));
      form.pid = form.category_id[0] ? form.category_id[0] : "";
      form.category_id = category(form.category_id);
      if (form.direct_supply) {
        form.supplier_id = form.direct_supply;
      }
      delete form.direct_supply;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, form, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form,
        this.current
      );
      purchaseRelation_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.list.data.map((e) => {
            e.down = false;
            e.purchase_relation.forEach((p) => {
              for (const key in p) {
                if (p[key] == 0) {
                  p[key] = "";
                }
              }
            });
            e.purchase_list = JSON.parse(JSON.stringify(e.purchase_relation));
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    purchaseRelation_base() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
          ),
        },
        apiUrl().name
      );
      purchaseRelation_base(data).then((res) => {
        if (res.errcode == 0) {
          this.options = recursion(res.data.categoryList, true);
          this.type = res.data.enable_user_relationship == 1 ? true : false;
          this.info = res.data;
          this.info.supplier_list = res.data.supplier;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";
.main_table .el-table .cell, .main_table .el-table--border td:first-child .cell, .main_table .el-table--border th:first-child .cell{
  padding-left: 12px;
}
.mainor_form form {
  width: calc(100% - 260px);
}
.flex-center {
  display: flex;
  align-items: center;
  .navbar_svg {
    margin-left: 5px;
  }
}

.maincla_sxf {
  margin-right: 15px;
  margin-left: 10px;
  svg {
    display: block;
    &:nth-child(2) {
      margin-left: -4px;
      margin-top: 5px;
    }
    &:hover {
      cursor: pointer;
    }
  }
  use {
    fill: #c1c1c1ff;
  }
}
/deep/.main_dialog .el-checkbox {
  margin-bottom: 0;
}
.mainor_form {
  min-height: 70px;
}
.maincg_ul {
  ul {
    li {
      margin-left: 10px;
      margin-right: 40px;
      margin-top: 6px;
      margin-bottom: 6px;
      align-items: center;
      .main_page_top {
        margin-top: 7px;
        margin-bottom: 20px;
      }
      span {
        font-size: $font_14;
        color: $colour_3;
      }
      /deep/.el-select {
        margin-left: 5px;
      }
      .maincg_uli {
        margin-left: 5px;
        position: relative;
        .maincg_uli_s {
          position: absolute;
          right: 10px;
          top: 8px;
          z-index: 9;
          use {
            fill: $menuText;
          }
          &:hover {
            cursor: pointer;
            use {
              fill: $menuActiveText;
            }
          }
        }
      }
    }
  }
}
.maincg_bj {
  margin-left: -30px;
  p {
    margin-top: 2px;
    margin-bottom: 2px;
    span {
      display: inline-block;
      width: 56px;
      text-align: right;
    }
    margin-left: 30px;
    color: $menuText;
    font-size: $font_14;
  }
}

.main_dialog {
  /deep/.el-form--inline .el-form-item__label {
    display: none;
  }
  .main_table {
    /deep/.el-table__body-wrapper {
      overflow-y: auto;
      height: 300px;
    }
  }
}

.wid100 {
  width: 100px;

  /deep/.el-input__inner {
    border: none;
  }
}

/deep/.mainsu_te {
  bottom: 5px;
}
/deep/.el-form-item--small.el-form-item {
  margin-bottom: 10px;
}
</style>
